

var nameFlag = false;
var pwFlag = false;
var imageCodeFlag = false;
var emailFlag = true;


$(document).ready(function(){
	
	//登录
	initLoginBut();
	initLoginMsg();
	
	//注册
	initRegUserName();
	initResPw();
	initRegEmail();
	initImageCode();
	initRegSubmit();
	
	initWantRisTag();
	
	//找回密码
	initFindPwTag();

});

function initFindPwTag(){
	$("#forgetPw").click(function(){
		$("#findPwDiv").css("visibility","visible");
		$("#login_div").slideUp("normal");
	});
	
	var flag = false;
	
	$("#findPwEmail").blur(function(){
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
		var email = $(this).val();
	
		if(email != ""){
			if(re.test(email)){
				flag = true;
			}else{
				showErr("findPwMsg","邮箱格式不正确");
				flag = false;
			}
			
			if(flag){
				$.ajax({
			        type: "post",
			        url: $('#basePath').val()+"checkEmail?user.email="+$("#findPwEmail").val(),
			        data: {},
			        success:function(data,status) {
			        	if(data.isExist){
			        		showCorrect("findPwMsg","√");
			        		flag = true;
			        	}else{
			        		showErr("findPwMsg","此邮箱未注册");
			        		flag = false;
			        	}
			        },
			        error:function(data,status){
			        	alert("ajax error.");
			        }
			     });
			}
		}
	});
	
	
	$("#findPwSubmit").click(function(){
		if(flag){
			$.ajax({
		        type: "post",
		        url: $('#basePath').val()+"findPwByEmail?user.email="+$("#findPwEmail").val(),
		        data: {},
		        success:function(data,status) {
		        	if(data=="success"){
			        	showCorrect("findSucSpan","我们已经向此邮箱发送了一封邮件，请去邮箱确认");
		        	}else{
		        		showErr("findSucSpan","发生未知错误");
		        	}
		        },
		        error:function(data,status){
		        	alert("ajax error.");
		        }
		     });
		}else{
			if($("#findPwEmail").val()==""){
				showErr("findPwMsg","请填写邮箱");
			}
		}
	});
}


function initLoginMsg(){
	var hintMsg = "用户名/邮箱";
	
	$("#loginName").val(hintMsg);
	$("#loginName").addClass("inner_input");
	
	$("#loginName").focus(function(){
		if($(this).val()==hintMsg){
			$(this).val("");
		}
		$("#loginName").removeClass("inner_input");
	});
	
	$("#loginName").blur(function(){
		if($(this).val()==""){
			$(this).val(hintMsg);
			$("#loginName").addClass("inner_input");
		}else{
			$("#loginName").removeClass("inner_input");
		}
		
		$("#loginNameValidMsg").css("visibility","hidden");
	});
	
	$("#loginPw").blur(function(){
		$("#loginPwValidMsg").css("visibility","hidden");
	});
}

function initLoginBut(){
	$("#loginBut").click(function(){
		if(validateLogin()){
			var flag = validateInfoBeforeSubmit();
			if(flag){
				$("#loginForm").submit();
			}
			else{
				showErr("loginNameValidMsg","用户名或密码输入有误");
			}
		}
	});
}


function validateInfoBeforeSubmit(){
	
	var flag = true;
	
	$.ajax({
        cache: false,
        async: false, 
        type: "post",
        url: $('#basePath').val()+"validateUserInfoAction",
        data: {
        	"user.username":$("#loginName").val(),
        	"user.password":$("#loginPw").val(),
        },
        success:function(data,status) {
        	
        	if(data!="exist"){
        		flag = false;
        	}
        },
        error:function(data,status){
        	flag = false;
        	alert("ajax error.");
        }
     });
	
	return flag;
}

function validateLogin(){
	
	var hintMsg = "用户名/邮箱";
	
	var name = $("#loginName").val();
	var pw = $("#loginPw").val();
	var flag = false;
	if(name=="" || name==hintMsg){
		showErr("loginNameValidMsg","输入用户名");
		flag = true;
	}
	if(pw==""){
		showErr("loginPwValidMsg","输入密码");
		flag = true;
	}
	var imageCode = $("#securityCode").val();
	
	if(imageCode==""){
		showErr("vCodeMsg","输入验证码");
		flag = true;
	}
	
	if(!imageCodeFlag){
		flag = true;
	}
	
	if(flag){
		return false;
	}
	
	return true;
}


//点击注册按钮，需要做的事情有：
//1.显示注册Div
//2.给邮件Span添加normalclass
//3.login_div slideup
function initWantRisTag(){
	$("#wantRis").click(function(){
		$("#regDiv").css("visibility","visible");
		$("#EmlValidMsg").addClass("normalMessage");	
		$("#login_div").slideUp("normal");
	});
}

function initRegSubmit(){
	$("#registSubmit").click(function(){
		
		if(nameFlag && pwFlag && emailFlag){
			
			$.ajax({
		        type: "post",
		        url: $('#basePath').val()+"registerAction",
		        data: {
		        	"user.username":$("#usernameText").val(),
		        	"user.password":$("#pw1").val(),
		        	"user.email":$("#userEmailText").val()
		        },
		        success:function(data,status) {
		        	
		        	if(data=="success"){
		        	showCorrect("regResSpan","注册成功，现在返回登录界面");
		        	setTimeout(function(){
		        		$("#login_div").slideDown("slow",function(){
		        			$("#regDiv").css("visibility","hidden");
		        			$("#nameValidMsg").css("visibility","hidden");
		        			$("#pwValidMsg").css("visibility","hidden");
		        			$("#pwReValidMsg").css("visibility","hidden");
			        		$("#EmlValidMsg").css("visibility","hidden");
			        		$("#regResSpan").css("visibility","hidden");
		        		});
		        	},1000);

		        	}else{
		        		showErr("regResSpan","填写有误或发生未知错误");
		        	}
		        },
		        error:function(data,status){
		        	alert("ajax error.");
		        }
		     });
		}
		//validate register message 
		validateRegMsg();
	});
}

function initRegUserName(){
	
	//确定用户名的正则表达式：长度为2-16，不包含特殊字符
	var userNamePattren =  /^[\u4e00-\u9fa5\w]{2,16}$/ ;	
	
	$("#usernameText").blur(function(){
		
		var name = $("#usernameText").val();
		
		if(name != ""){
			
			if(userNamePattren.test(name)){
				nameFlag = true;
			}else{
				showErr("nameValidMsg","长度为2-20，不能包含特殊字符");
				nameFlag = false;
			}
			//到数据库进行验证，要求姓名必须唯一
			if(nameFlag){
				$.ajax({
			        type: "post",
			        url: $('#basePath').val()+"checkUserName?user.username="+encodeURI(encodeURI($("#usernameText").val())),
			        data: {},
			        success:function(data,status) {
			        	if(data.isExist){
			        		showErr("nameValidMsg","此用户名已被占用");
			        		nameFlag = false;
			        	}else{
			        		showCorrect("nameValidMsg","√");
			        		nameFlag = true;
			        	}
			        },
			        error:function(data,status){
			        	alert("ajax error.");
			        }
			     });
			}
		}else{
    		$("#nameValidMsg").css("visibility","hidden");
    		nameFlag = false;
		}
	});
}

function initResPw(){
	var pwPat = /^(\w){6,20}$/;			//密码正则表达式
	
	$("#pw1").blur(function(){
		var pw = $(this).val();
		
		if(pw != ""){
			if(!pwPat.test(pw)){
				showErr("pwValidMsg","请输入6-20位字母数字或下划线");
				pwFlag = false;
			}else{
        		$("#pwValidMsg").css("visibility","hidden");
        		pwFlag = true;
			}
		}else{
			$("#pwValidMsg").css("visibility","hidden");
			pwFlag = false;
		}
		
	});
	
	$("#pw2").blur(function(){
		var pw1 = $("#pw1").val();
		var pw2 = $(this).val();
		var pw1vis = $("#pwValidMsg").css("visibility");
		
		//pw1显示错误信息是，Pw2不进行错误提示
		
		if(pw2==""){
			pwFlag = false;
			return;
		}
		
		if(pw1 != pw2 && pw1vis != "visible"){
			showErr("pwReValidMsg","两次输入密码不一致");
			pwFlag = false;
		}else if(pw1 != pw2){
			pwFlag = false;
		}else{
			$("#pwReValidMsg").css("visibility","hidden");
			pwFlag = true;
		}
	});
}

function initRegEmail(){
	$("#userEmailText").blur(function(){
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
		var email = $(this).val();
		if(email != ""){
			if(re.test(email)){
	    		emailFlag = true;
			}else{
				showErr("EmlValidMsg","邮箱格式不正确");
	    		emailFlag = false;
			}
			
			//到数据库进行验证，要求邮箱必须唯一
			if(emailFlag){
				$.ajax({
			        type: "post",
			        url: $('#basePath').val()+"checkEmail?user.email="+$("#userEmailText").val(),
			        data: {},
			        success:function(data,status) {
			        	if(data.isExist){
			        		showErr("EmlValidMsg","此邮箱已被注册");
			        		emailFlag = false;
			        	}else{
			        		showCorrect("EmlValidMsg","√");
			        		emailFlag = true;
			        	}
			        },
			        error:function(data,status){
			        	alert("ajax error.");
			        }
			     });
			}
		}else{
			emailFlag = true;
			$("#EmlValidMsg").css("visibility","hidden");
		}
	});
}

function initImageCode(){
	$("#securityCodeImg").click(function(){
		 $("#vCodeMsg").css("visibility","hidden");
		 $(this).attr("src","SecurityCodeImageAction?timestamp="+new Date().getTime());
	});
	
	$("#securityCode").blur(function(){
		var secureCode = $(this).val();
		if(secureCode != ""){
			$.ajax({
		        type: "post",
		        url: $('#basePath').val()+"checkSecureCode?securityCode="+secureCode,
		        data: {},
		        success:function(data,status) {
		        	if(!data.isCorrect){
		        		showErr("vCodeMsg","验证码输入有误");
		        		imageCodeFlag = false;
		        	}else{
		        		showCorrect("vCodeMsg","√");
		        		imageCodeFlag = true;
		        	}
		        },
		        error:function(data,status){
		        	alert("ajax error.");
		        }
		     });
		}else{
			$("#vCodeMsg").css("visibility","hidden");
			imageCodeFlag = false;
		}
	});
}

function validateRegMsg(){
	//usernameText pw1 pw2 userEmailText securityCode
	
	if($("#usernameText").val()==""){
		showErr("nameValidMsg","输入用户名");
	}
	
	if($("#pw1").val()==""){
		showErr("pwValidMsg","请输入密码");
	}
	
	if($("#pw1").val()!="" && $("#pw2").val()==""){
		showErr("pwReValidMsg","请再输入密码");
	}
	
	var msgSpanCss = $("#EmlValidMsg").css("visibility");
	
	if(!emailFlag && msgSpanCss=="hidden"){
		showErr("EmlValidMsg","邮箱格式不正确");
	}
}

function showCorrect(id,msg){
	$("#"+id).text(msg);
	$("#"+id).removeClass("errorMessage").addClass("correctMessage");
	$("#"+id).css("visibility","visible");
}

function showErr(id,msg){
	$("#"+id).text(msg);
	$("#"+id).removeClass("correctMessage").addClass("errorMessage");
	$("#"+id).css("visibility","visible");
}

